#include "RawData.hh"
#include "TCanvas.h"
#include <string>
#include <sstream>


//--------noise histo---------//
//TH1F* voltage_noise = new TH1F("voltage noise", "Voltage noise", 100, -0.1, 0.1); 
//---------------------------//

//float max_v = 0;
//float min_v = 0;


RawData::RawData(string fname, HeaderParameters* h, int channels)
  :filename(fname),
   header(h)
{
	my_channels=channels;
  waveforms = new TH1F*[2];

  datafile = new ifstream(filename.c_str(), ifstream::in);
  waveforms[0] = NULL;
  waveforms[1] = NULL;

}


RawData::~RawData(){

	delete waveforms;

}



#include "TMath.h"

TH1F** RawData::LoadNextWaveforms(float time_per_div){

  int channel;
	for (channel=0; channel<my_channels; channel++)
	{
	
	
	  if (datafile->eof())
	    return NULL;

	  float volts_per_sample = header->GetParameter(channel, 0);	
	  float volts_offset = header->GetParameter(channel,1);	
	  float scale = TMath::Power(2,header->GetParameter(channel,2));
	  int num_samples = int(header->GetParameter(channel,3));
	  float time_per_sample = header->GetParameter(channel,5);
	  
	  float baseline_sum=0;
	  float baseline_avg;
	  int nbaseline = num_samples/10;
	  
	  float v = 0;
	  string csv_buffer; 
	  string csv_buffer_stripped; 
 
	  if (waveforms[channel]==NULL){
		std::stringstream ch;
		ch << channel+1;
		std::string name = "waveform" + ch.str();
	    waveforms[channel] = new TH1F(name.c_str(), "Waveform (V vs. secs)", num_samples, 0, float(num_samples)*time_per_sample);
	    waveforms[channel]->SetXTitle("time");
	    waveforms[channel]->SetYTitle("Volts");
	  }
	
	  waveforms[channel]->Reset();
	  
	  for (int n = 0; n<num_samples; n++){
//		    getline(*datafile, csv_buffer, ',' );
			*datafile >> v;
//		    v = atof(csv_buffer.c_str());  
		    
//		    if (v>=scale/2)
//		      v -= scale; //This could have to do with bringing out of range voltages in range
		    
		    if (n<nbaseline){
		      baseline_sum += volts_per_sample*(v-volts_offset); //Increment baseline voltage using 1st 10% of trace
		      v = volts_per_sample*(v - volts_offset); // Scale and offset these voltages in 1st 10%
			  
		    }

		    if (n==nbaseline){
		      baseline_avg = baseline_sum/float(nbaseline);
		    }	
		    if (n>=nbaseline){
		      v = volts_per_sample*(v - volts_offset); //Scale and offset the other 90% of voltages
		      v -= baseline_avg;  //Apply baseline offset to the non-1st-10% voltages
		    }
		    waveforms[channel]->SetBinContent(n+1,v);
	  }

//		string crap;
	
//		    getline(*datafile, crap, ',' );

//		cout << "CRAP: " << crap << endl;
	  
	  //The following loop adds entries to the waveforms histograms, but the number of bins remains the same
	  
	  for (int n=1; n<nbaseline+1; n++){
	    waveforms[channel]->SetBinContent(n, waveforms[channel]->GetBinContent(n) - baseline_avg ); //Apply baseline offset to voltages in 1st 10% of the trace 
	  }
	  
	}
	return waveforms; 
}

void RawData::PlotWaveform(){

  float ns = 1.0e9;
  TCanvas* c= new TCanvas("c", "waveform", 600, 400);
  c->Draw();
  
  for(int channel=0;channel<my_channels;channel++)
    {
      waveforms[channel]->Draw();
      c->Update();
      
      cout << " Waveform Integral: " << waveforms[channel]->Integral()/50.0/ns/1.6e-19 << " e-" << endl;
      
      cout << "Hit [ENTER] to continue." << endl;
      cin.get();
    }  
  
  c->Clear();
  delete c;
  c = NULL;
  
  
}
